package Demo05.Demo02;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Demo01_RewardPool {
    public static void main(String[] args) {
        Reward reward = new Reward();
        new Thread(reward,"积分夺宝").start();
        new Thread(reward,"钻石夺宝").start();
    }
}
class Reward implements Runnable{

    int[] arr = {10,5,20,50,100,200,500,800,2,80,300};
    List<Integer> list = new ArrayList<>();
    @Override
    public void run() {
        while (true){
            lott();
            if (list.size()==arr.length){
                break;
            }
        }
    }

    private synchronized void lott() {
        int nextInt = new Random().nextInt(arr.length);
        if (!list.contains(nextInt)){
            list.add(nextInt);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                System.out.println(e);
            }
            String name = Thread.currentThread().getName();
            if (name.equals("奖品一"))
                System.out.println(name+"又产生了一个"+arr[nextInt]+"元大奖");
            else
                System.out.println(name+"又产生了一个"+arr[nextInt]+"元大奖");
        }
    }
}
